package com.cqw.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.*;

@Data
@NoArgsConstructor
public class SysUser implements UserDetails, Serializable {


    private static final long serialVersionUID = -4102892043312986827L;

    @ApiModelProperty(value = "userId")
    private Long userId;

    @ApiModelProperty(value = "userName用户名")
    private String userName;

    @ApiModelProperty(value = "password密码")
    private String password;

    @ApiModelProperty(value = "email邮箱")
    private String email;

    @ApiModelProperty(value = "mobile手机号")
    private String mobile;

    @ApiModelProperty(value = "status状态  0：禁用   1：正常")
    private Integer status;

    @ApiModelProperty(value = "createBy创建者ID")
    private Long createBy;

    @ApiModelProperty(value = "createTime创建时间")
    private Date createTime;

    @ApiModelProperty(value = "updateBy修改者ID")
    private Long updateBy;

    @ApiModelProperty(value = "updateTime修改时间")
    private Date updateTime;

    @ApiModelProperty(value = "shopId用户所在的商城Id")
    private Long shopId;

    @ApiModelProperty(value = "是否被删除：1正常，2删除")
    private Integer isDelete;


    @ApiModelProperty(value = "用户权限")
    private List<String> permissions;

    private String token;

    /**
     * 权限
     *
     * @return
     */
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        if (CollectionUtils.isEmpty(this.permissions)) {
            return Collections.emptyList();
        }

        permissions.forEach(auth -> {
            if (StringUtils.isNotBlank(auth)) {
                //分割
                String[] split = auth.split(",");
                for (String s : split) {
                    authorities.add(new SimpleGrantedAuthority(s));
                }
            }
        });
        return authorities;
    }

    /**
     * 获取用户id
     *
     * @return
     */
    @JsonIgnore
    @Override
    public String getUsername() {
        return this.userName;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return this.password;
    }

    /**
     * 判断账号是否过期
     *
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    /**
     * 判断账号是否被锁
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}